package com.gyt.manager.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.gyt.manager.common.annotation.ElasticSimpleJob;
import com.gyt.manager.common.base.response.Page;
import com.gyt.manager.dao.risk.DalMetaLabelInfoMapper;
import com.gyt.manager.dao.risk.LabelInfoMapper;
import com.gyt.manager.domain.risk.DalMetaLabelInfo;
import com.gyt.manager.domain.risk.LabelInfo;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * description.
 *
 * @author gyt create on 2021/5/19 18:21
 */
@ElasticSimpleJob(cron = "0 3 * * * ?", jobName = "labelTestJob")
@Component
public class LabelTestJob implements SimpleJob {
    private static final Logger LOGGER = LoggerFactory.getLogger(LabelTestJob.class);
    @Autowired
    @Qualifier("taskExecutor.label.collect")
    private ThreadPoolTaskExecutor executor;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private DalMetaLabelInfoMapper dalMetaLabelInfoMapper;
    @Override
    public void execute(ShardingContext shardingContext) {
//        long start = System.currentTimeMillis();
//        AtomicInteger count = new AtomicInteger(0);
//        final int[] markCnt = {0};
//        List<LabelInfo> list = Lists.newArrayList();
//        try {
//            List<String> labelIds;
//            int pageSize = 500, pageNum = 1;
//            List<Future<?>> futures = new ArrayList<>();
//            do {
//                Page.startPage(pageNum++, pageSize);
//                labelIds = labelInfoMapper.selectAllLabelId();
//                List<String> finalLabelIds = labelIds;
//                int finalPageNum = pageNum;
//                Future<?> future = executor.submit(() -> {
//                    if (!CollectionUtils.isEmpty(finalLabelIds)) {
//                        for (String labelId : finalLabelIds) {
//                            try {
//                                Thread.sleep((long) (Math.random()*20));
//                            } catch (InterruptedException e) {
//                                LOGGER.error("sleep failed,thread interrupted", e);
//                            }
//                            DalMetaLabelInfo metaLabelInfo = dalMetaLabelInfoMapper.selectByLabelId(labelId);
//                            if(null != metaLabelInfo) {
//                                LabelInfo info = new LabelInfo();
//                                info.setFlabelId(labelId);
//                                info.setFlabelName(metaLabelInfo.getFlabelName());
//                                info.setFlabelLevel(metaLabelInfo.getFlabelLevel());
//                                list.add(info);
//                            }
//                        }
//                        count.addAndGet(list.size());
//                        markCnt[0] += list.size();
//                        LOGGER.info("current page done! pageNum={},cnt={}",
//                                finalPageNum, list.size());
//                    }
//                });
//                futures.add(future);
//
//            } while (labelIds.size() >= pageSize);
//            LOGGER.info("executor end, count=[{}], cost:[{}]ms",
//                    count, System.currentTimeMillis() - start);
//        } catch (Exception e) {
//            LOGGER.error("executor error", e);
//            throw e;
//        } finally {
//            LOGGER.info("executor end, markCnt=[{}], cost:[{}]ms",
//                    markCnt[0], System.currentTimeMillis() - start);
//        }
    }
}
